// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#ifndef __PLANETARYTILE_H_
#define __PLANETARYTILE_H_

#include "planetaryloadable.h"
#include <OgreSceneNode.h>

namespace Ogre
{
	class PlanetaryRenderable;
	class PlanetarySceneManager;
	class PlanetaryPage;

	/**
	* Manages a tile, the smallest most detailed terrain part of the scene manager
	* A tile is created by its parent page and is added to the loading queue  by the parent page when the camera gets too close.
	*/
	class PlanetaryTile : public PlanetaryLoadable
	{
	public:
		PlanetaryTile(PlanetarySceneManager *mgr);
		~PlanetaryTile();

		/// Sets the tile up ready to get loaded
		void init(unsigned int zone, unsigned int pageX, unsigned int pageZ, unsigned int tileX, unsigned int tileZ, PlanetaryPage *parent);
		
		/// Loads the tile
		void load();

		/// Unloads the tile
		void unload();

		/// Uninit function
		void uninit();

		/// Show or hide the tile.
		/// @param do_it If set to true, shows the tile
		void show(bool do_it);

		/// Set the material to use
		/// @param mat The material to use when rendering
		void setMaterial(MaterialPtr mat);
		
		/// Returns if the tile is currently loaded
		inline bool isLoaded()
		{
			return mIsLoaded;
		}
		
		/// Get the parent page
		inline PlanetaryPage *getParent()
		{
			return mParent;
		}

	private:
		/// True if the tile is laoded and ready to be rendered
		bool mIsLoaded;
		/// True if the page is initialised and ready to be loaded
		bool mIsInit;

		/// Parent zone ID
		unsigned int mZone;
		/// Parent page X ID
		unsigned int mPageX;
		/// Parent page Z ID
		unsigned int mPageZ;
		/// The tile X ID
		unsigned int mTileX;
		/// The tile Z ID
		unsigned int mTileZ;

		/// Tile scene node
		SceneNode *mTileNode;

		/// Scene Manager object
		PlanetarySceneManager *mSceneMgr;

		/// Parent Page
		PlanetaryPage *mParent;

		/// The tile's renderable
		PlanetaryRenderable *mRenderable;
	};
}

#endif
